package com.xkcyy.one.controller;

import com.xkcyy.one.common.Result;
import com.xkcyy.one.entity.Agent;
import com.xkcyy.one.entity.Training;
import com.xkcyy.one.service.TrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 训练控制器
 */
@RestController
@RequestMapping("/api/trainings")
public class TrainingController {
    
    private final TrainingService trainingService;
    
    @Autowired
    public TrainingController(TrainingService trainingService) {
        this.trainingService = trainingService;
    }
    
    /**
     * 获取所有训练
     *
     * @return 训练列表
     */
    @GetMapping("/list")
    public Result<List<Training>> getAllTrainings() {
        return Result.success(trainingService.getAllTrainings());
    }
    
    /**
     * 根据智能体ID获取训练列表
     *
     * @param agentId 智能体ID
     * @return 训练列表
     */
    @GetMapping("/agent")
    public Result<List<Training>> getTrainingsByAgentId(@RequestParam Long agentId) {
        return Result.success(trainingService.getTrainingsByAgentId(agentId));
    }
    
    /**
     * 根据ID获取训练
     *
     * @param id 训练ID
     * @return 训练信息
     */
    @GetMapping("/get")
    public Result<Training> getTrainingById(@RequestParam Long id) {
        return Result.success(trainingService.getTrainingById(id));
    }
    
    /**
     * 创建训练
     *
     * @param training 训练信息
     * @return 创建后的训练信息
     */
    @PostMapping("/create")
    public Result<Training> createTraining(@RequestBody Training training) {
        return Result.success(trainingService.createTraining(training));
    }
    
    /**
     * 更新训练基本信息
     *
     * @param id       训练ID
     * @param training 训练信息
     * @return 更新后的训练信息
     */
    @PutMapping("/update")
    public Result<Training> updateTraining(@RequestParam Long id, @RequestBody Training training) {
        training.setId(id);
        return Result.success(trainingService.updateTraining(training));
    }
    
    /**
     * 删除训练
     *
     * @param id 训练ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Result<Void> deleteTraining(@RequestParam Long id) {
        trainingService.deleteTraining(id);
        return Result.success();
    }
    
    /**
     * 开始训练
     * 
     * @param id 训练ID
     * @return 更新后的训练信息
     */
    @PostMapping("/start")
    public Result<Training> startTraining(@RequestParam Long id) {
        return Result.success(trainingService.startTraining(id));
    }
    
    /**
     * 应用训练结果到智能体
     *
     * @param id 训练ID
     * @return 更新后的智能体信息
     */
    @PostMapping("/apply")
    public Result<Agent> applyTrainingResult(@RequestParam Long id) {
        return Result.success(trainingService.applyTrainingResult(id));
    }
} 